﻿using Microsoft.Xna.Framework.Graphics;

namespace Lab3.Entities
{
    class SnowPlow : Entity
    {
        private BasicEffect _effect;
        private GraphicsDevice _device;

        public SnowPlow(Camera camera, BasicEffect effect, GraphicsDevice device) : base(camera)
        {
            _effect = effect;
            _device = device;
        }

        public override void Draw(int type)
        {
            if (type == GraphicsConstants.NormalDraw)
            {
                _device.RasterizerState = RasterizerState.CullClockwise;

                foreach (ModelMesh mesh in Model.Meshes)
                {
                    foreach (ModelMeshPart part in mesh.MeshParts)
                    {
                        _effect = (BasicEffect)part.Effect;
                        _effect = SceneBasicEffects(_effect);

                        if (((BasicEffect)part.Effect).TextureEnabled)
                        {
                            _effect.Texture = Texture;
                            _effect.TextureEnabled = true;
                        }

                        _effect.View = Camera.View;
                        _effect.Projection = Camera.Projection;
                        _effect.World = ObjectWorld;

                        part.Effect = _effect;
                        part.Effect.Techniques[0].Passes[0].Apply();
                    }
                    if (mesh.Name != "Circle.003" && mesh.Name != "Circle.004")
                    {
                        _device.RasterizerState = RasterizerState.CullClockwise;
                        mesh.Draw();
                        _device.RasterizerState = RasterizerState.CullCounterClockwise;
                        mesh.Draw();
                    }
                }
            }

            if (type == GraphicsConstants.AlphaDraw)
            {
                _device.BlendState = BlendState.AlphaBlend;
                foreach (ModelMesh mesh in Model.Meshes)
                {
                    if (mesh.Name == "Circle.003" || mesh.Name == "Circle.004")
                    {
                        foreach (ModelMeshPart part in mesh.MeshParts)
                        {
                            _effect = (BasicEffect)part.Effect;
                            _effect = SceneBasicEffects(_effect);

                            _effect.Alpha = 0.3f;
                            _effect.View = Camera.View;
                            _effect.Projection = Camera.Projection;
                            _effect.World = ObjectWorld;
                            _effect.EnableDefaultLighting();
                            part.Effect = _effect;
                            part.Effect.CurrentTechnique.Passes[0].Apply();
                        }
                        _device.RasterizerState = RasterizerState.CullClockwise;
                        mesh.Draw();
                        _device.RasterizerState = RasterizerState.CullCounterClockwise;
                        mesh.Draw();
                    }
                }
                _device.BlendState = BlendState.Opaque;
            }
        }
    }
}
